package bhbt.service.log;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.log.LogChuangye;
import bhbt.model.log.LogKuang;
import bhbt.model.log.LogKuangPay;
import bhbt.model.log.LogMoney;
import bhbt.model.log.LogStock;
import bhbt.service.DailiService;
import bhbt.service.DailiShengService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.WageCurrentService;
import bhbt.service.core.BaseService;
import bhbt.utils.ContextUtil;






@Service
public class LogKuangService extends BaseService {
	
	 
	@Resource
    private MemberService memberService;
	@Resource
    private DailiService dailiService;
	@Resource
    private DailiShengService dailiShengService;
	@Resource
    private LogMoneyService logMoneyService;
	@Resource
    private ParamWageService paramWageService;
	@Resource
    private LogChuangyeService logChuangyeService;
	@Resource
    private LogStockService logStockService;
	@Resource
    private LogKuangPayService logKuangPayService;
	@Resource
    private WageCurrentService wageCurrentService;
	
	
	public void saveBuyKuang(Integer num,Integer wageType ){
		 
		
		
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		ParamWage paramWage1003 = this.paramWageService.getByKey(1003);
		
		Boolean result = false;
		
		if(wageType == 10){
			Double money = num * paramWage1003.getValue();
			
			if(member.getMoney()>=money){
				Map<String, Object> mapMember = new HashMap<String, Object>();
				mapMember.put("id",member.getId());
				mapMember.put("diyUpdate","money=money-"+money+",kuang=kuang+"+num+",buyKuang=buyKuang + "+num);
		        //更新余额 
		        memberService.update(mapMember);
		        
		        
		        member = this.memberService.getByKey(member.getId());
		        Double moneyYu = member.getMoney();
		        //日志
		        LogMoney logMoney = new LogMoney();
		        logMoney.setMemberId(member.getId());
		        logMoney.setTitle("购买矿点，购买数："+num+"，矿点价格："+paramWage1003.getValue());
		        logMoney.setState(88);
		        logMoney.setTotalNum(money);
		        logMoney.setNum(-money);
		        logMoney.setYu(moneyYu);
		        logMoney.setDongNum(0d);
		        logMoney.setFaDate(new Date());
		        logMoneyService.saveOrUpdate(logMoney);
		        
		       
		        
		        result=true;
			}
			
			
		}
		
		
		/*else if(wageType == 20){
			
			Double chuangye = num * paramWage1003.getValue();
			
			if(member.getChuangye()>=chuangye){
				Map<String, Object> mapMember = new HashMap<String, Object>();
				mapMember.put("id",member.getId());
				mapMember.put("diyUpdate","chuangye=chuangye-"+chuangye+",kuang=kuang+"+num+",buyKuang=buyKuang + "+num+",stock=stock+"+stock);
		        //更新余额 
		        memberService.update(mapMember);
		        
		        member = this.memberService.getByKey(member.getId());
		        Double chuangyeYu = member.getChuangye();
		        //日志
		        LogChuangye logChuangye = new LogChuangye();
		        logChuangye.setMemberId(member.getId());
		        logChuangye.setTitle("购买矿点，购买数："+num+"，矿点价格："+paramWage1003.getValue());
		        logChuangye.setState(88);
		        logChuangye.setNum(-chuangye);
		        logChuangye.setYu(chuangyeYu);
		        logChuangyeService.saveOrUpdate(logChuangye);
		        
		        
		        //赠送原始股
		        Double stockYu = member.getChuangye();
				LogStock logStock = new LogStock();
	            logStock.setMemberId(member.getId());
	            logStock.setTitle("购买矿点，获得原始股，购买矿点数："+num);
	            logStock.setState(88);
	            logStock.setNum(stock);
	            logStock.setYu(stockYu);
	            logStockService.saveOrUpdate(logStock);
				
		        
		        result=true;
			}
			
		}*/
		
		
		if(result){
			//产矿天数
			ParamWage paramWage1001 = this.paramWageService.getByKey(1001);
			double chanDayDouble = paramWage1001.getValue();
			Integer chanDay = (int)chanDayDouble;
			
			for(int i = 1 ;i<=num ;i++){
				
				Random random = new Random(); 
				SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss"); 
				String kuangCodeNo="K"+random.nextInt(9)
						+ random.nextInt(9) 
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9)
						+ random.nextInt(9);
				
				LogKuang log = new LogKuang();
				 
				log.setMemberId(member.getId());
				log.setKuangCodeNo(kuangCodeNo);
				log.setChanDay(chanDay);
				log.setYuDay(chanDay);
				this.saveOrUpdate(log);
			}
			
			
			
			//修改剩余数
			Map<String, Object> mapParamWage = new HashMap<String, Object>();
			mapParamWage.put("id", 801);
			mapParamWage.put("diyUpdate", " value = value -"+num);
			this.paramWageService.update(mapParamWage);
			
			
		}
		 
		
	}
	
	public void saveKuangActive(String[] ids){
		
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		 
		Integer num = 0;
		for(String kuangIdString:ids){
			int kuangId = Integer.parseInt(kuangIdString);
			LogKuang lk  = this.getByKey(kuangId);
			
			//判断该矿点是否是本人的
			if(lk.getMemberId().equals(member.getId())){
					
				//激活矿点
				 
				Map<String, Object> mapLogKuang = new HashMap<String, Object>();
				mapLogKuang.put("id",kuangId);
				mapLogKuang.put("isActive","T");
				mapLogKuang.put("activeDate",new Date());
				this.update(mapLogKuang);
				  
		        num++;
			}
			
		}
		
		
		
		if(num>0){
			ParamWage paramWage1003 = this.paramWageService.getByKey(1003);
			Double wageKuangActive = num * paramWage1003.getValue();
			
			//增加累计激活
			Map<String, Object> mapMember = new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate","payNum = payNum + "+wageKuangActive +",activeKuang = activeKuang +"+num+",totalActiveKuang=totalActiveKuang+"+num);
			this.memberService.update(mapMember);
			
			//会员升级
			member= memberService.getByKey(member.getId());
			Daili daili20=this.dailiService.getByKey(20);
			Daili daili30=this.dailiService.getByKey(30);
			if(member.getTotalActiveKuang()>=daili20.getKuangShengNum()&&member.getDailiId()<20){
				this.dailiShengService.saveSheng(member, daili20);
			}
			
			if(member.getTotalActiveKuang()>=daili30.getKuangShengNum()&&member.getDailiId()<30){
				this.dailiShengService.saveSheng(member, daili30);
			}
			
			member= memberService.getByKey(member.getId());
			Daili daili = this.dailiService.getByKey(member.getDailiId());
			 
			 
			//升级后增加原始股
			Double stock =num* daili.getGuNum()+0.0;
			mapMember = new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate"," stock = stock +"+stock);
			this.memberService.update(mapMember);
			
			member= memberService.getByKey(member.getId());
			 //赠送原始股
			Double stockYu = member.getStock();
			LogStock logStock = new LogStock();
			logStock.setMemberId(member.getId());
			logStock.setTitle("购买矿点，获得原始股，购买矿点数："+num);
			logStock.setState(88);
			logStock.setNum(stock);
			logStock.setYu(stockYu);
			logStockService.saveOrUpdate(logStock);
			
			
			//奖金计算(招商佣金)
			Member recommend = this.memberService.getByKey(member.getRecommendId());
			if(recommend!=null){
				Daili dailiRe = this.dailiService.getByKey(recommend.getDailiId());
				Double wage1 =  wageKuangActive*dailiRe.getTuiNum1()*0.01;
				this.memberService.saveCountWage1(recommend, member, wage1,num, wageKuangActive);
				
				
				Map<String, Object> mapRecommend = new HashMap<String, Object>();
				mapRecommend.put("id", member.getRecommendId());
				mapRecommend.put("diyUpdate", "recommendYe=recommendYe+"+wageKuangActive);
				this.memberService.update(mapRecommend);
			}
			 
			/*******************************一条线增加业绩******************************/
			
			Integer reId=member.getRecommendId();
			Member re = memberService.getByKey(reId);
			//直推代数奖金
			Integer cengRe=1;
			  
			while(re!=null){		
				
				 
				Map<String, Object> mapRecommend = new HashMap<String, Object>();
				mapRecommend.put("id", reId);
				mapRecommend.put("diyUpdate", " recommendTotalYe=recommendTotalYe+"+wageKuangActive);
				memberService.update(mapRecommend);				 					
				 
			  
				//当天记录
				Integer wageCurrentId = re.getWageCurrentId();
				Map<String, Object> mapWageCurrent = new HashMap<String, Object>();
				mapWageCurrent.put("id", wageCurrentId);
				mapWageCurrent.put("diyUpdate", "recommendTotalYe=recommendTotalYe+"+wageKuangActive);
				
				wageCurrentService.update(mapWageCurrent);
				  
				reId=re.getRecommendId();
				re = memberService.getByKey(reId);
				cengRe=cengRe+1;
				
			}
			
			
			
			
			/*****************  系统参数的激活业绩**********************/
			Map mapParamWage=new HashMap<String, Object>();
			mapParamWage.put("id", 2001);//今日激活
			mapParamWage.put("diyUpdate", "value=value+"+wageKuangActive);
			paramWageService.update(mapParamWage);	
			
			
			mapParamWage=new HashMap<String, Object>();
			mapParamWage.put("id", 2003);//累计激活
			mapParamWage.put("diyUpdate", "value=value+"+wageKuangActive);
			paramWageService.update(mapParamWage);	
			
			
			
			
			
			
			
		}
		
		
		
		
		
	}
	
	
	public void saveKuangPay(String[] ids,Member toMember,String content){
		
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		Integer num = 0;
		
		for(String kuangIdString:ids){
			int kuangId = Integer.parseInt(kuangIdString);
			LogKuang lk  = this.getByKey(kuangId);
			
			//判断该矿点是否是本人的
			if(lk.getMemberId().equals(member.getId())){
				//转让操作
				Map<String, Object> mapLogKuang = new HashMap<String, Object>();
				mapLogKuang.put("id",kuangId);
				mapLogKuang.put("memberId",toMember.getId());
				this.update(mapLogKuang);
				
				//转让记录
				LogKuangPay lkp = new LogKuangPay();
				lkp.setFromMemberId(member.getId());
				lkp.setToMemberId(toMember.getId());
				lkp.setKuangCodeNo(lk.getKuangCodeNo());
				lkp.setContent(content);
				this.logKuangPayService.saveOrUpdate(lkp);
				
				
				num++;
			}
			
		}
		
		
		//会员矿数修改
		Map<String, Object> mapMember = new HashMap<String, Object>();
		mapMember.put("id",toMember.getId());
		mapMember.put("diyUpdate"," kuang = kuang +"+num);
		this.memberService.update(mapMember);
		
		
		mapMember = new HashMap<String, Object>();
		mapMember.put("id",member.getId());
		mapMember.put("diyUpdate"," kuang = kuang -"+num);
		this.memberService.update(mapMember);
		
		
		
	}
	
	
	
	public void saveKuangAgain(Integer num){
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		ParamWage paramWage1003 = this.paramWageService.getByKey(1003);
		Double chuangye = num * paramWage1003.getValue();
		
		if(member.getChuangye()>=chuangye){
			Map<String, Object> mapMember = new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate","chuangye=chuangye-"+chuangye+",kuang=kuang+"+num+",buyKuang=buyKuang + "+num+" ,activeKuang = activeKuang +"+num);
	        //更新余额 
	        memberService.update(mapMember);
	        
	        member = this.memberService.getByKey(member.getId());
	        Double chuangyeYu = member.getChuangye();
	        //日志
	        LogChuangye logChuangye = new LogChuangye();
	        logChuangye.setMemberId(member.getId());
	        logChuangye.setTitle("购买矿点，购买数："+num+"，矿点价格："+paramWage1003.getValue());
	        logChuangye.setState(88);
	        logChuangye.setNum(-chuangye);
	        logChuangye.setYu(chuangyeYu);
	        logChuangyeService.saveOrUpdate(logChuangye);
	         
            
            //产矿天数
    		ParamWage paramWage1001 = this.paramWageService.getByKey(1001);
    		double chanDayDouble = paramWage1001.getValue();
    		Integer chanDay = (int)chanDayDouble;
    		
    		for(int i = 1 ;i<=num ;i++){
    			
    			Random random = new Random(); 
    			String kuangCodeNo="K"+random.nextInt(9)
    					+ random.nextInt(9) 
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9)
    					+ random.nextInt(9);
    			
    			LogKuang log = new LogKuang();
    			log.setMemberId(member.getId());
    			log.setKuangCodeNo(kuangCodeNo);
    			log.setChanDay(chanDay);
    			log.setYuDay(chanDay);
    			log.setIsActive("T");
    			log.setActiveDate(new Date());
    			this.saveOrUpdate(log);
    		}
	        
		}
		
		
	}

}

















